package cn.itsource.pethome.basic.util;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {

    //加密
    public static String encrypByMd5(String context) {
        try {  
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(context.getBytes());
            byte [] encryContext = md.digest();
  
            int i;  
            StringBuffer buf = new StringBuffer("");  
            for (int offset = 0; offset < encryContext.length; offset++) {//做相应的转化（十六进制）  
                i = encryContext[offset];  
                if (i < 0) i += 256;  
                if (i < 16) buf.append("0");  
                buf.append(Integer.toHexString(i));  
           }  
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
            return  null;
        }  
    }

    public static void main(String[] args) {
        //原文加密
        System.out.println(MD5Utils.encrypByMd5("1"));
        //盐值加密
            System.out.println(MD5Utils.encrypByMd5("1AgX8sq85NJ"));
        //不同盐值加密
        System.out.println(MD5Utils.encrypByMd5("1sunfulin"));
        //加密2次
        System.out.println(MD5Utils.encrypByMd5(MD5Utils.encrypByMd5("1sunfulin")));
    }

}